import { _decorator, Component, Node, ProgressBar, sp, UITransform } from 'cc';
import { Battle } from '../../Battle';
import { EnDynamicLineType } from '../../Common/const/EnCommon';
import { EnEvent } from '../../Common/const/EnEvent';
import { GameAPI } from '../../Common/GameAPI';
import { Player } from '../../Player/Player';
import { EnemyController, EnemyFrameAnimType, EnemyFrameInfo } from './EnemyController';
const { ccclass, property } = _decorator;

@ccclass('Boss3Controller')
export class Boss3Controller extends EnemyController {

    @property(Node)
    public hpProgress: Node = null;

    private hpNode: UITransform;
    private hpMaxWidth: number = 0;

    onChildInit() {
        this.hpNode = this.hpProgress.getComponent(UITransform);
        this.hpMaxWidth = this.hpNode.width;
        this.attackInterval = 1.5;
        this.hp = 1500;
        this.hurt = 150;
        this.isBoss = true;
        let info: EnemyFrameInfo = new EnemyFrameInfo();
        info.animType = EnemyFrameAnimType.Idle;
        info.startIndex = 0;
        info.endIndex = 7;
        info.isLoop = true;
        info.speed = 0.04;
        info.frameName = "Cyclop_Idle_";
        info.rootPath = "Frame/Enemy/Boss3/Idle";
        this.frameInfoList.push(info);

        info = new EnemyFrameInfo();
        info.animType = EnemyFrameAnimType.Run;
        info.startIndex = 0;
        info.endIndex = 15;
        info.isLoop = true;
        info.speed = 0.04;
        info.frameName = "Cyclop_Walk_";
        info.rootPath = "Frame/Enemy/Boss3/Walk";
        this.frameInfoList.push(info);

        info = new EnemyFrameInfo();
        info.animType = EnemyFrameAnimType.Attack;
        info.startIndex = 0;
        info.endIndex = 15;
        info.frameName = "Cyclop_Attack_";
        info.rootPath = "Frame/Enemy/Boss3/Attack";
        this.frameInfoList.push(info);

        info = new EnemyFrameInfo();
        info.animType = EnemyFrameAnimType.Die;
        info.startIndex = 0;
        info.endIndex = 15;
        info.frameName = "Cyclop_Death_";
        info.rootPath = "Frame/Enemy/Boss3/Death";
        this.frameInfoList.push(info);
    }

    
    OnUpdateHP() {
        if (this.hpProgress) {
            let width = this.hpMaxWidth * (this.hp / this.totalHP);
            this.hpNode.setContentSize(width, this.hpNode.height);
        }
    }
    
    OnDieCallback() {
        if (this.hpProgress) {
            this.node.active = false;
        }
        GameAPI.FireGEvent(EnEvent.GameWinEvent);
        GameAPI.FireGEvent(EnEvent.DynamicLineEvent, { type: EnDynamicLineType.Default });
    }

    AttackedPlayerInjured() {
        this.scheduleOnce(function () {
            let player: Player = Battle.playerNode.getComponent(Player);
            player.Injured(this.hurt);
        }.bind(this), 0.5);
    }
}


